package com.springnews.utils;

import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

@SuppressWarnings("unchecked")
@Component
public class NewsJob {
	@Resource
	private RedisTemplate redisTemplate;
	private final Logger logger= LoggerFactory.getLogger(this.getClass());
	@Scheduled(cron = "0 0/1 * * * ?")
	public void SaveNewsData(){
		Set<String> keys=redisTemplate.keys("news_*");
		for (String key : keys) {
			Map<String,String>map=redisTemplate.opsForHash().entries(key);
			String id=key.substring(5);
			String read=map.get("read");
			String zan=map.get("zan");
			logger.info("编号："+id+"，阅读次数："+read+"，点赞次数："+zan);
		}
		logger.info("以上数据已保存到数据库");
	}
}
